<?php


namespace api\channel\meituan\alliance;


use app\middleware\MeituanCityNameToCityId;
use app\model\BaseSupplier;
use app\model\MeituanCity;
use GaoweiSpace\MeituanPubUnion\Api\Common\Request\SeckillListRequest;
use GaoweiSpace\MeituanPubUnion\Api\Common\Request\SeckillShowInfoRequest;
use GaoweiSpace\MeituanPubUnion\Api\Tool\Request\OrdersCPSRequest;
use GaoweiSpace\MeituanPubUnion\Api\Tool\Request\OrdersCPSRequestNew;
use GaoweiSpace\MeituanPubUnion\Http\Client;
use think\Exception;


/**
 * Class Seckill
 * @package api\channel\meituan\alliance
 * 美团分销联盟文档：
 * https://pub.meituan.com/#/api-doc
 */
class Seckill extends MeituanAllianceBase
{

    public $client;
    public $code;
    private $params;

    public function __construct(array $params = [])
    {
        $this->code = BaseSupplier::CODE_MEITUANMIAOSHA;

        $this->params = $params;

        //判断是否返佣还是接口付费
        parent::__construct(PRODUCT_SECKILL_SERV, $this->code);

        if (empty($this->supplierInfo->api_config['utmSource'])) {
            throw new Exception('不存在配置信息utmSource');
        }

        $this->client = new Client($this->apikey, $this->supplierInfo->api_config['utmSource']);
    }

    /**
     * 秒杀列表
     * https://inews.gtimg.com/newsapp_ls/0/14902932679/0.jiketuchuang.png
     */
    public function seckillList()
    {

        $agent_id = $this->params['agent_id'] ?? 0;
        $user_id = $this->params['user_id'] ?? 0;
        $channel_id = $this->params['channel_id'] ?? 0;

        $cityName = $this->params['city_name'] ?? '';
        if ($cityName) {
            $cityName = $this->params['city_name'] ?? '';
            if ($cityName) {
                $this->params["cityId"] = $this->getCityIdByCityName($cityName);
            }

        }

        $request = new SeckillListRequest();


        $request->setCityId($this->params["cityId"] ?? 0);
        $request->setShowId($this->params['showId']);

        $request->setOs($this->params['os'] ?? 99);

        $request->setLat($this->params['lat'] ?? 39.918018);
        $request->setLng($this->params['lng'] ?? 116.397034);


        $request->setPage($this->params['page'] ?? 1);

        $request->setPhone($this->client->_encrypt($this->params['phone'] ?? '15080206817'));
        $request->setUtmMedium($this->client->_encrypt($this->getSid($agent_id, $user_id, $channel_id, PRODUCT_SECKILL_SERV)));

        $response = $this->client->syncInvoke($request);

        $data = $response->getBody();

        return json_decode($data, true);

    }

    /**
     * 秒杀时间列表
     * @return array
     */
    public function seckillShowInfo()
    {
        $cityName = $this->params['city_name'] ?? '';
        if ($cityName) {
            $this->params["cityId"] = $this->getCityIdByCityName($cityName);
        }


        $request = new SeckillShowInfoRequest();

        $request->setCityId($this->params["cityId"] ?? MeituanCityNameToCityId::MEITUAN_DDEFAULT_CITYID);

        $response = $this->client->syncInvoke($request);

        $data = $response->getBody();

        return json_decode($data, true);

    }


    public function getCityIdByCityName($cityName)
    {
        $cityId = 1;
        $cityName = str_replace('市', '', $cityName);
        if ($cityName) {
            $cityId = MeituanCity::query()->where('name', $cityName)
                ->where('city_id', '<>', 0)
                ->value('city_id', 0);
        }
        return $cityId;

    }

    public function orders($params)
    {
        $request = new OrdersCPSRequest();

        $request->setPage($params['page'] ?? 1);
        $request->setSize(1000);

        $response = $this->client->syncInvoke($request);

        $data = $response->getBody();
        //同步
        $data = json_decode($data, true);

        $res['data'] = $data['msg']['records'] ?? [];

        if (empty($res['data'])) {
            var_dump($data);
        }

        return $res;
    }
}